var param=window.location.href.split("=")[1];
Ext.setup({
    icon:'icon.png',
    tabletStartupScreen:'tablet_startup.png',
    phoneStartupScreen:'phone_startup.png',
    glossOnIcon:false,
    onReady:function () {
        var form;
 
	   	 Ext.regModel('agenymapmodel', {
	   	    fields: [
	   	        {name: 'AGENCYID', type: 'string'},
	   	        {name: 'NAME', type: 'string'}
	   	    ]
	   	});
		
	   	 Ext.regModel('detailycostmodel', {
		   	    fields: [
		   	        {name: 'DEPARTMENT', type: 'string'},
		   	        {name: 'COMMITER', type: 'string'},
		   	        {name: 'COMMITERCODE', type: 'string'}
		   	    ]
		 });
	   	 
		var agenymapStore = new Ext.data.Store({
			autoLoad: true,
		    model: 'agenymapmodel',
		    proxy: {
		        type: 'ajax',
		        url : '../../dic/dic_agencyMap.action',
		        reader: {
		            type: 'json'
		        }
		    }
		});
		
		Ext.Ajax.request({
            url: '../../costmanagement/dailyListCost_getUserDepartment.action',
            success: function(response, opts) {
       	     var jsonObj = eval("(" + response.responseText + ")");
       	    	var user = Ext.ModelMgr.create({
       	    		COMMITER  : jsonObj.COMMITER,
       	    		DEPARTMENT : jsonObj.DEPARTMENT,
       	    		COMMITERCODE:jsonObj.COMMITERCODE
       	    	}, 'detailycostmodel');
       	    
       	    	form.load(user);
            }
        });
		
        var formBase = {
            scroll:'vertical',
            standardSubmit:false,
            items:[
                {
                    xtype:'fieldset',
                    title:'所属单位',
                    items:[
                        {
                            xtype:'selectfield',
                            name:'OWNERDEPT',
                            label:'单位名称',
    						xtype : 'selectfield',
    						name : 'AGENCYID',
    						label : '单位名称',
    						placeHolder : '请选择所属单位',
    						valueField: 'AGENCYID',
    						displayField:'NAME',
    						store : agenymapStore,
    						required : true
                        }
                    ]
                },
                {
                    xtype:'fieldset',
                    title:'基本信息',
                    instructions:'请输入报销编号',
                    defaults:{
                        required:true,
                        labelAlign:'left',
                        labelWidth:'40%'
                    },
                    items:[
                        {
                            xtype:'textfield',
                            name:'DEPARTMENT',
                            label:'提交人部门',
                            value:'',
                            useClearIcon:false
                        },
                        {
                            xtype:'textfield',
                            name:'COMMITER',
                            label:'提交人',
                            value:'',
                            useClearIcon:false
                        },
                        {
                            xtype:'textfield',
                            name:'COMMITERCODE',
                            label:'提交人编号',
                            value:'',
                            useClearIcon:false
                        }
                    ]
                },
                {
                    xtype:'fieldset',
                    title:'报销信息',
                    defaults:{
                        xtype:'fieldset'
                    },
                    items:[
                        {
                            xtype:'textfield',
                            name:'APPLIERNAME',
                            label:'申请人',
                        },
                        {
                            xtype:'textfield',
                            name:'AMOUNT',
                            label:'申请金额',
                            value:'',
                            useClearIcon:false
                        },
                        {
                            xtype:'textareafield',
                            name:'DESCRIBE',
                            label:'申请事由',
                            maxLength:60,
                            maxRows:3
                        }
                    ]
                }
            ],
            listeners:{
                submit:function (form, result) {
                    console.log('success', Ext.toArray(arguments));
                },
                exception:function (form, result) {
                    console.log('failure', Ext.toArray(arguments));
                }
            },

            dockedItems:[
                {
                    xtype:'toolbar',
                    dock:'bottom',
                    items:[
                        {
                            xtype:'spacer'
                        },
                        {
                            text:'重置',
                            handler:function () {
                                form.reset();
                            }
                        },
                        {
                            text:'保存',
                            ui:'confirm',
                            handler:function () {
                            	Ext.Msg.confirm('系统提示','确定要提交？',function(btn){
                    		        if(btn=='yes'){
              								var url="../../costmanagement/dailyCostinsert.action?param="+param;
              								formBase.submitform(url);
                    		        }else{
                    		        	return;
                    		        }
                            		        
                            	},this);
                            }
                        }
                    ]
                }
            ],
            
            submitform:function(url){
            	form.submit( {
    				waitMsg : {
    					message : '正在提交...',
    					cls : 'demos-loading'
    				},
    			 url: url,           
    			 method: 'POST',           
    			 success: function(thisForm, action){ 
    					Ext.Msg.alert('提示', '提交成功',function(btn){
    						document['success'] = 'true';
    						//window.location.href='../../costmanagement/dailyCostSubmit/dailyCostList.jsp';
    					});	
    					
    				}
    			});
            }
        };
        
        if (Ext.is.Phone) {
            formBase.fullscreen = true;
        } else {
            Ext.apply(formBase, {
                autoRender:true,
                floating:true,
                modal:true,
                centered:true,
                hideOnMaskTap:false,
                height:385,
                width:480
            });
        }

        form = new Ext.form.FormPanel(formBase);
        form.show();
    }
});